#if defined(ARDUINO_FEATHER_ESP32) || defined(ESP32) // Feather Huzzah32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#endif
#include <WiFiClient.h>

#ifndef STASSID
#define STASSID "Xiaomi_2G_free" //你的wifi名称
#define STAPSK  ""  //你的wifi密码  我家没有密码所以没有了
#endif

const char* ssid = STASSID;
const char* password = STAPSK;
bool relay_s =  false;
ESP8266WebServer server(80);

const int led = 13;
const int relay1 = 0;

void handleRoot() {
  digitalWrite(led, 1);
  server.send(200, "text/plain", "hello from esp8266!\r\n");
  digitalWrite(led, 0);
}

void sw() {
  relay_s = ! relay_s;
  digitalWrite(led, 1);
  digitalWrite(relay1, relay_s);
  String sout = "relay is";
  sout += (relay_s == true) ? " on\r\n" : " off\r\n";
  server.send(200, "text/plain", sout);
  digitalWrite(led, 0);
}


void relayon() {
  digitalWrite(led, 1);
  digitalWrite(relay1, 0);
  server.send(200, "text/plain", "relay is on\r\n");
  digitalWrite(led, 0);
}


void relayoff() {
  digitalWrite(led, 1);
  digitalWrite(relay1, 1);
  server.send(200, "text/plain", "relay is off\r\n");
  digitalWrite(led, 0);
}



void handleNotFound() {
  digitalWrite(led, 1);
  String message = "File Not Found\n\n";
  message += "URI: ";
  message += server.uri();
  message += "\nMethod: ";
  message += (server.method() == HTTP_GET) ? "GET" : "POST";
  message += "\nArguments: ";
  message += server.args();
  message += "\n";
  for (uint8_t i = 0; i < server.args(); i++) {
    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
  }
  server.send(404, "text/plain", message);
  digitalWrite(led, 0);
}

void setup(void) {
  pinMode(led, OUTPUT);
  pinMode(relay1, OUTPUT); //

  digitalWrite(relay1, 1);
  digitalWrite(led, 1);

  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  if (MDNS.begin("esp8266")) {
    Serial.println("MDNS responder started");
  }

  server.on("/", handleRoot);
  server.on("/sw", sw);
  server.on("/on", relayon);
  server.on("/off", relayoff);
  server.on("/inline", []() {
    server.send(200, "text/plain", "this works as well");
  });


  server.onNotFound(handleNotFound);


  server.begin();
  Serial.println("HTTP server started");

}

void loop(void) {
  server.handleClient();
  MDNS.update();
}